package flc.ast.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.DrawerPopupView;
import com.lxj.xpopup.enums.PopupPosition;

import java.util.List;

import flc.ast.R;
import flc.ast.adapter.ModuleManageAdapter;
import flc.ast.bean.ResumeBean;
import flc.ast.utils.ResumeDataUtils;

public class ModuleManageDrawer extends DrawerPopupView implements View.OnClickListener {

    public int position;
    private RecyclerView recyclerView;
    private ModuleManageAdapter moduleManageAdapter;
    private List<ResumeBean> listResume;
    private TextView tvNum;

    public ModuleManageDrawer(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.drawer_module_manage;
    }

    @Override
    protected void onCreate() {
        findViewById(R.id.ivSort).setOnClickListener(this);
        findViewById(R.id.ivCopy).setOnClickListener(this);
        findViewById(R.id.ivAdd).setOnClickListener(this);
        findViewById(R.id.ivDelete).setOnClickListener(this);
        tvNum = findViewById(R.id.tvNum);

        listResume = ResumeDataUtils.getResumeList();
        tvNum.setText((position + 1) + "/" + listResume.size());
        recyclerView = findViewById(R.id.rvModuleList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        moduleManageAdapter = new ModuleManageAdapter();
        recyclerView.setAdapter(moduleManageAdapter);
        moduleManageAdapter.setList(listResume.get(position).getResumeData());
        moduleManageAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (position == 0) return;
                if (moduleManageAdapter.getItem(position).isShow()) {
                    moduleManageAdapter.getItem(position).setShow(false);
                } else {
                    moduleManageAdapter.getItem(position).setShow(true);
                }
                moduleManageAdapter.notifyDataSetChanged();
                ResumeDataUtils.saveResumeList(listResume);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivSort://排序
                SortDrawer sortDrawer = new SortDrawer(getContext());
                sortDrawer.setListener(new SortDrawer.Ilistener() {
                    @Override
                    public void clickConfirm() {
                        listResume = ResumeDataUtils.getResumeList();
                        moduleManageAdapter.setList(listResume.get(position).getResumeData());
                    }
                });
                new XPopup.Builder(getContext())
                        .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                        .popupPosition(PopupPosition.Left)//左边
                        .hasStatusBarShadow(true) //启用状态栏阴影
                        .asCustom(sortDrawer)
                        .show();
                break;
            case R.id.ivCopy://复制
                showCopyDialog();
                break;
            case R.id.ivAdd://添加
                if (listResume.size() == 12) {
                    ToastUtils.showShort(R.string.create_resume_tip1);
                    return;
                }
                ResumeBean resumeBean = new ResumeBean();
                resumeBean.setResumeData(ResumeDataUtils.getModuleList());
                listResume.add(resumeBean);
                ResumeDataUtils.saveResumeList(listResume);
                ToastUtils.showShort(getContext().getString(R.string.create_resume_suc));
                dismiss();
                break;
            case R.id.ivDelete://删除
                showDeleteDialog();
                break;
        }
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        if (listener != null) {
            listener.refresh();
        }
    }

    private void showCopyDialog() {
        CopyDialog copyDialog = new CopyDialog(getContext());
        copyDialog.setListener(new CopyDialog.Ilistener() {
            @Override
            public void clickConfirm() {
                ResumeBean resumeBean = listResume.get(position);
                listResume.add(resumeBean);
                ResumeDataUtils.saveResumeList(listResume);
                tvNum.setText((position + 1) + "/" + listResume.size());
                ToastUtils.showShort(getContext().getString(R.string.copy_suc));
            }
        });
        copyDialog.show();
    }

    private void showDeleteDialog() {
        DeleteDialog deleteDialog = new DeleteDialog(getContext());
        deleteDialog.setListener(new DeleteDialog.Ilistener() {
            @Override
            public void clickConfirm() {
                listResume.remove(position);
                ResumeDataUtils.saveResumeList(listResume);
                dismiss();
                ToastUtils.showShort(getContext().getString(R.string.delete_suc));
                if (listener != null) {
                    listener.clickDelete();
                }
            }
        });
        deleteDialog.show();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void refresh();

        void clickDelete();
    }

}
